package org.iteam.xlz.core.service.product;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.product.ProductFreight;

public class ProductFreightService extends BaseService<ProductFreight>{
	static ProductFreightService instance;
	private List<Object> params=new ArrayList<Object>();
	private StringBuilder sql=null;
	
	private ProductFreightService(){}
	
	public static ProductFreightService getInstance(){
		if(null==instance){
			instance=new ProductFreightService();
		}
		return instance;
	}
	
	public long counts(ProductFreight model){
		params.clear();
		sql=new StringBuilder("select count(1) from product_freight where 1=1");
		if(StringUtils.isNotBlank(model.getFreightCode())){
			sql.append(" and freightCode like ?");
			params.add("%"+model.getFreightCode()+"%");
		}
		if(StringUtils.isNotBlank(model.getId())){
			sql.append(" and id like ?");
			params.add("%"+model.getId()+"%");
		}
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<ProductFreight> query(ProductFreight model,long page,long rows){
		params.clear();
		sql=new StringBuilder("select * from product_freight where 1=1");
		if(StringUtils.isNotBlank(model.getFreightCode())){
			sql.append(" and freightCode like ?");
			params.add("%"+model.getFreightCode()+"%");
		}
		if(StringUtils.isNotBlank(model.getId())){
			sql.append(" and id like ?");
			params.add("%"+model.getId()+"%");
		}
		sql.append(" order by id desc limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<ProductFreight>();
		}
	}
	
	public boolean create(ProductFreight model){
		String id=UUID.randomUUID().toString().replace("-","").toUpperCase();
		model.setId(id);
		SqlBuilder builder=SqlBuilder.createInsert("product_freight");
		builder.addField("id",model.getId());
		builder.addField("freightDesc",model.getFreightDesc());
		builder.addField("freightCode",model.getFreightCode());
		try {
			return super.updates(builder.getSql(),builder.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(ProductFreight model){
		SqlBuilder builder=SqlBuilder.createUpdate("product_freight");
		builder.addField("freightDesc",model.getFreightDesc());
		builder.addField("freightCode",model.getFreightCode());
		builder.addWhere("id",model.getId());
		try {
			return super.updates(builder.getSql(),builder.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public ProductFreight findOne(String id){
		try {
			return super.show("select * from product_freight where 1=1 and id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ProductFreight();
		}
	}
	
	public boolean delete(String id){
		try {
			return super.updates("delete from product_freight where id=?",id)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
